package com.zui.util;

import java.io.Serializable;


public class Page implements Serializable {
	private static final long serialVersionUID = 6215981491167447762L;

	public final static int PAGE_SIZE = 15;
	public static final String PAGE = "page";
	public static final String SORT_COLUMN = "sort";
	public static final String DEFAULT_SORT_COLUMN = "id";

	public static final String SORT_DIRECTION_ASCENDING = "asc";
	public static final String SORT_DIRECTION_DESCENDING = "desc";

	public static final String TYPE_EXT = "EXT";
	public static final String TYPE_DISPLAYTAG = "DISPLAYTAG";
	public static final String TYPE_JQGRID = "JQGRID";

	public Page() {
		requestPage = 1;
		this.type = TYPE_DISPLAYTAG;
	}

	public Page(String requestPage) {
		if (requestPage != null)
			this.requestPage = new Integer(requestPage).intValue();
		else
			this.requestPage = 1;
		this.type = TYPE_DISPLAYTAG;
	}

	public Page(String requestPage, int pageSize) {
		this(requestPage);
		this.pageSize = pageSize;
		this.type = TYPE_DISPLAYTAG;
	}

	public Page(int requestPage, int pageSize) {
		this.requestPage = requestPage;
		this.pageSize = pageSize;
		this.type = TYPE_DISPLAYTAG;
	}
	/**
	 * The constructor for ExtJs remote paing and sorting
	 * @param firstResult The first record index.
	 * @param pageSize The size of the page.
	 * @param sortDirection Which direction the column will be sorted on.
	 * @param sortColumn Which column will be sorted on.
	 */
	public Page(int firstResult, int pageSize, String sortDirection, String sortColumn) {
		this.type = TYPE_EXT;
		this.firstResult = firstResult;
		this.pageSize = pageSize;
		this.sortColumn = sortColumn;
		this.sortDirection = sortDirection;
	}

	/**
	 * the number of rows requested
	 */
	protected int pageSize = PAGE_SIZE;
	/**
	 * the the requested page
	 */
	protected int requestPage;
	protected String sortColumn;
	protected String sortDirection;
	protected int firstResult;
	/**
	 * The page class now support displaytag paging, Extjs paging and jqGrid paging.
	 * 标记出来有什么用呢？
	 */
	protected String type;

	/**
	 * get the index of first result according to the requestPage and pageSize.
	 * if the type of page class is TYPE_EXT,
	 * 
	 * @return
	 */
	public int getFirstResult() {
		if (TYPE_DISPLAYTAG.equalsIgnoreCase(type)) {
			if (requestPage == 0)
				requestPage = 1;

			firstResult = (requestPage - 1) * pageSize;
		}
		return firstResult;
	}

	/**
	 * 
	 * @return
	 */
	public String getSortColumn() {
		//Sorting is very performance exhausting, the id sorting
		//is different from default database sorting
//		if (sortColumn == null || sortColumn.trim().equals(""))
//			sortColumn = DEFAULT_SORT_COLUMN;
		return sortColumn;
	}

	/**
	 * 
	 * @return
	 */
	public String getSortDirection() {
		//Sorting is very performance exhausting, the id sorting
		//is different from default database sorting
//		if (sortDirection == null || sortDirection.trim().equals(""))
//			sortDirection = SORT_DIRECTION_ASCENDING;
		return sortDirection;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getRequestPage() {

		return requestPage;
	}

	public void setRequestPage(int requestPage) {
		this.requestPage = requestPage;
	}

	public void setSortColumn(String sortColumn) {
		this.sortColumn = sortColumn;
	}

	public void setSortDirection(String sortDirection) {
		this.sortDirection = sortDirection;
	}

	public void setFirstResult(int firstResult) {
		this.firstResult = firstResult;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
}
